using System;

namespace Rob.SampleCode.MyApp.Core.Domain
{
    public class OrderDetail : IEquatable<OrderDetail>
    {
        protected OrderDetail()
        {
        }

        public OrderDetail(Order order, Product product, decimal unitPrice, short quantity, float discount)
        {
            Order = order;
            Product = product;
            UnitPrice = unitPrice;
            Quantity = quantity;
            Discount = discount;
        }

        public virtual Order Order { get; protected set; }
        public virtual Product Product { get; protected set; }
        public virtual decimal UnitPrice { get; set; }
        public virtual short Quantity { get; set; }
        public virtual float Discount { get; set; }

        public virtual bool Equals(OrderDetail other)
        {
            if (ReferenceEquals(null, other)) return false;
            if (ReferenceEquals(this, other)) return true;
            return other.Order.OrderID == Order.OrderID && other.Product.ProductID == Product.ProductID;
        }

        public override bool Equals(object obj)
        {
            return Equals(obj as OrderDetail);
        }

        public override int GetHashCode()
        {
            unchecked
            {
                return (Order.OrderID * 397) ^ Product.ProductID;
            }
        }

        public static bool operator ==(OrderDetail left, OrderDetail right)
        {
            return Equals(left, right);
        }

        public static bool operator !=(OrderDetail left, OrderDetail right)
        {
            return !Equals(left, right);
        }
    }
}